<?php 

class CommonController extends MY_Controller {
	public $CurModel;
	function __construct(){
		parent::__construct();
		
		
		$this->CurModel = null;
		$this->err_msg = '';
	}

	function getError(){
		return $this->err_msg;
	}
	
	function _get_list($f=array(),$append_arr = array()){

		$user_id = $this->user_id;

		$page = $this->input->post('page',null,1);
		$size = $this->input->post('size',null,10);
		$filter = $this->input->post('f',null,array());
		
		$f_rr = isset($f['filter'])?$f['filter']:array();
		
		
		foreach($append_arr as $key=>$val){
			$filter[$key] = $val;
			$f_rr[] = $key;
		}
		
		
		
		foreach($filter as $key=>$val){
			if(!in_array($key,$f_rr)){
				$this->err_msg = '参数输入不允许';
				return false;
			}
		}
		
		$arr = array();
		foreach($filter as $key=>$val){
			$arr[$key] = $val;
		}

		$res = $this->CurModel->get_list($arr,$page,$size);
		$list = $res['list'];

		
		foreach($list as $key=>$val){
			$list[$key] = $this->info_out($val);
		}
		$res['list'] = $list;
		
		
		return $res;
	}

	
	function _checkData($f){
		
		$f_arr = isset($f['post'])?$f['post']:array();
		
		$post = $this->input->post();
		
		$arr = array();
		foreach($post as $key=>$val){
			if(in_array($key,$f_arr)){
				$arr[$key] = $val;
			}
		}
		$data = isset($f['filter'])?$f['filter']:array();
		if(count($data)>0){
			$result = $this->CheckData($data,$arr);
			
			if($result!==false){
				$this->err_msg = $result;
				return false;
			}
		}
		return true;
	}
	//post
	//filter
	function _save($f,$append_arr = array(),$is_update=false){
		
		$field = isset($f['field'])?$f['field']:'id';
		$id = $this->input->post($field,null,0);
		$f_arr = isset($f['post'])?$f['post']:array();
		
		$post = $this->input->post();
		
		$arr = array();
		foreach($post as $key=>$val){
			if(in_array($key,$f_arr)){
				$arr[$key] = $val;
			}
		}
		
		$data = isset($f['filter'])?$f['filter']:array();
		if(count($data)>0){
			$result = $this->CheckData($data,$arr);
			if($result!==false){
				$this->err_msg = $result;
				return false;
			}
		}
		
		
		$exists = isset($f['exists'])?$f['exists']:array();
		if(count($exists)>0){
			$ls_arr = array();
			foreach($exists as $val){
				if(isset($arr[$val])){
					$ls_arr[$val] = $arr[$val];
				}
			}
			
			$result = $this->CurModel->_where_exist($ls_arr);
			if($result){
				$this->err_msg = '当前数据已存在';
				return false;
			}
		}
		
		if(!empty($id)){
			$row = $this->CurModel->_id_get_info($id);
			if(!$row){
				$this->err_msg = '数据错误';
				return false;
			}
			if($is_update){
				foreach($append_arr as $key=>$val){
					$arr[$key] = $val;
				}
			}
			
			$this->CurModel->edit($arr,"id='{$id}'");
			$arr['id'] = $id;
		}
		else{
			
			foreach($append_arr as $key=>$val){
				$arr[$key] = $val;
			}
			
			$arr['id'] = $this->CurModel->add($arr);
		}
		
		return $arr;
	}
	function info_out($info){
		return $info;
	}

	
	function _get_info($f=array()){
		
		$field = isset($f['field'])?$f['field']:'id';
		
		$id = $this->input->post($field,null,0);
		
		$info = $this->CurModel->_id_get_info($id);
		if(!$info){
			$this->err_msg = '数据不存在';
			return false;
		}
		
		$info = $this->info_out($info);
		return $info;
	}
	

	function _del($f=array()){
		
		$field = isset($f['field'])?$f['field']:'id';
		$id = $this->input->post($field,null,0);
		
		$info = $this->CurModel->_id_get_info($id);
		if(!$info){
			$this->err_msg = '数据不存在';
			return false;
		}
		
		$this->CurModel->del("id='{$id}'");
		
		return $id;
	}
	
	//写入缓存
	function cache_write($name,$str){
		$file = APPPATH.'cache/all_'.$name.'.php';
		$cache_str="<?php\n\$".$name."=".var_export($cache_list,true).";\n?>";
		$fp=fopen($file,'w+');
		flock($fp,LOCK_EX);
		if(!fwrite($fp,$cache_str)){
			flock($fp,LOCK_UN);
			fclose($fp);
			$this->err_msg = "【{$name}】写入缓存失败";
			return false;
		}
		flock($fp,LOCK_UN);
		fclose($fp);
	}

	//写入缓存
	// function cache_write($file,$str,$name=''){
	// 	$fp=fopen($file,'w+');
	// 	flock($fp,LOCK_EX);
	// 	if(!fwrite($fp,$str)){
	// 		flock($fp,LOCK_UN);
	// 		fclose($fp);
	// 		$this->err_msg = "【{$name}】写入缓存失败";
	// 		return false;
	// 	}
	// 	flock($fp,LOCK_UN);
	// 	fclose($fp);
	// }

}
